<?php
class Company extends Kcontroller {
	function companylist() {
		$data = array();
		$data["error"] = Ksession::getFlash("error");
		$data["success"] = Ksession::getFlash("success");
		$this->load->model("companymodel");
		$data["company_list"] = $this->companymodel->getAllCompany();
		$this->load->template('templates/general','company/company_list', $this->k_application_title, $data);
	}
	
	function creation() {
		if (isset($_GET["comsel"]) && $_GET["comsel"]==1) :
			Ksession::setCriteria("comsel", 1);
		endif;
		$data = array();
		$data["error"] = Ksession::getFlash("error");
		
		$data["com_number"] = "";
		$data["com_name"] = "";
		$data["com_desc"] = "";
		$data["com_code"] = "";
		$data["com_vat_num"] = "";
		$data["com_address"] = "";
		$data["com_phone"] = "";
		$data["com_email"] = "";
		$data["com_fax"] = "";
		$data["comsel"] = 0;
		
		$comsel = Ksession::getCriteria("comsel");
		if ($comsel == 1) :
			$data["comsel"] = 1;
		endif;
		
		$flash_data = Ksession::getFlash("company");
		if ($flash_data != null) :
			$data = array_merge($data, $flash_data);
		endif;
		
		$this->load->template('templates/general','company/company_create', $this->k_application_title, $data);
	}
	
	function submitcreate() {
		$data["com_number"] = trim($_POST["com_number"]);
		$data["com_name"] = trim($_POST["com_name"]);
		$data["com_desc"] = trim($_POST["com_desc"]);
		$data["com_code"] = trim($_POST["com_code"]);
		$data["com_vat_num"] = trim($_POST["com_vat_num"]);
		$data["com_address"] = trim($_POST["com_address"]);
		$data["com_phone"] = trim($_POST["com_phone"]);
		$data["com_email"] = trim($_POST["com_email"]);
		$data["com_fax"] = trim($_POST["com_fax"]);
		
		$error = "";
		
		if ($data["com_number"] == "") :
			$error = "Company number cannot be empty";
		endif;
		
		if ($error == "" && $data["com_name"] == "") :
			$error = "Company name cannot be empty";
		endif;
		
		$this->load->model("companymodel");
		if ($error == "") :
			$company_found = $this->companymodel->getCompanyByNumber($data["com_number"]);
			if ($company_found != null) :
				$error = "Company number ".$data["com_number"]." is not available";
			endif;
		endif; 
		
		if ($error != "") :
			Ksession::setFlash("error", $error);
			Ksession::setFlash("company", $data);
			redirect(site_url("company/creation"));
			return;
		endif;
		
		$user = Ksession::getUser();
		$data["user_create"] = $user["user_id"];
		$com_id = $this->companymodel->createCompany($data);
		$comsel = Ksession::getCriteria("comsel");
		if ($comsel != null) :
			redirect(site_url("main/companyselection"));
			return;
		endif;
		Ksession::setFlash("success", "Company number ".$data["com_number"]." has been created successfully");
		redirect("company/companylist");
	}
	
	function companydel($com_id) {
		$this->load->model("companymodel");
		$this->companymodel->deleteCompany($com_id);
		redirect("company/companylist");
	}
	
	function edit($com_id) {
		$this->load->model("companymodel");
		$company = $this->companymodel->getCompanyById($com_id);

		$data = array();
		$data["error"] = Ksession::getFlash("error");
		$data["success"] = Ksession::getFlash("success");
		
		$data["com_id"] = $company["com_id"];
		$data["com_number"] =  $company["com_number"];
		$data["com_name"] =  $company["com_name"];
		$data["com_desc"] =  $company["com_desc"];
		$data["com_code"] =  $company["com_code"];
		$data["com_vat_num"] =  $company["com_vat_num"];
		$data["com_address"] =  $company["com_address"];
		$data["com_phone"] =  $company["com_phone"];
		$data["com_email"] =  $company["com_email"];
		$data["com_fax"] =  $company["com_fax"];
		
		
		
		$flash_data = Ksession::getFlash("company");
		if ($flash_data != null) :
			$data = array_merge($data, $flash_data);
		endif;
		
		$this->load->template('templates/general','company/company_edit', $this->k_application_title, $data);
		
	}
	
	function submitedit() {
		$data["com_id"] = $_POST["com_id"];
		$data["com_number"] = trim($_POST["com_number"]);
		$data["com_name"] = trim($_POST["com_name"]);
		$data["com_desc"] = trim($_POST["com_desc"]);
		$data["com_code"] = trim($_POST["com_code"]);
		$data["com_vat_num"] = trim($_POST["com_vat_num"]);
		$data["com_address"] = trim($_POST["com_address"]);
		$data["com_phone"] = trim($_POST["com_phone"]);
		$data["com_email"] = trim($_POST["com_email"]);
		$data["com_fax"] = trim($_POST["com_fax"]);
		
		$error = "";
		
		if ($data["com_number"] == "") :
			$error = "Company number cannot be empty";
		endif;
		
		if ($error == "" && $data["com_name"] == "") :
			$error = "Company name cannot be empty";
		endif;
		
		$this->load->model("companymodel");
		
		/*
		if ($error == "") :
			$company_found = $this->companymodel->getCompanyByNumber($data["com_number"]);
			if ($company_found != null) :
				$error = "Company number ".$data["com_number"]." is not available";
			endif;
		endif;
		*/
		
		if ($error != "") :
			Ksession::setFlash("error", $error);
			Ksession::setFlash("company", $data);
			redirect(site_url("company/edit/".$data["com_id"]));
			return;
		endif;
		
		$user = Ksession::getUser();
		$data["user_update"] = $user["user_id"];
		$com_id = $this->companymodel->updateCompany($data);
		
		
		Ksession::setFlash("success", "Company number ".$data["com_number"]." has been updated successfully");
		redirect("company/edit/".$data["com_id"]);
	}
}